﻿using Dapper;
using MLSMooc.BO;
using MLSMooc.DAO.Interface.Login;
using MLSMooc.Helper;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MLSMooc.DAO.Implement.Login
{
    public class UsersDAO : IUsersDAO
    {
        private static UsersDAO instance = new UsersDAO();
        private UsersDAO() { }
        public static UsersDAO Getinstance() => instance;

        private static readonly string queryField = $@"select * from Admin where isDelete=0";

        public async Task<UsersBO> GetByUNmAsync(string userNm)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                sql.Append($@" and AdminName=@userNm");
                var res = await conn.QueryAsync<UsersBO>(sql.ToString(), new { userNm = userNm });
                return res.FirstOrDefault();
            }
        }

        public async Task<UsersBO> GetUserAsync(string userNm, string pwd)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                sql.Append($@" and AdminName=@userNm and Password=@pwd");
                var res = await conn.QueryAsync<UsersBO>(sql.ToString(), new { userNm = userNm, pwd = pwd });
                return res.FirstOrDefault();
            }
        }

        public async Task<UsersBO> GetLabUserAsync(string userNm, string EncrptPwd)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                sql.Append($@" and AdminName=@userNm and EncryptPwd=@pwd");
                var res = await conn.QueryAsync<UsersBO>(sql.ToString(), new { userNm = userNm, pwd = EncrptPwd });
                return res.FirstOrDefault();
            }
        }

        public async Task<IEnumerable<UsersBO>> GetListAsync()
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                var res = await conn.QueryAsync<UsersBO>(sql.ToString());
                return res;
            }
        }

        public async Task<UsersBO> GetByIdAsync(string id)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryField);
                sql.Append($@"and Id=@id");
                var res = await conn.QueryAsync<UsersBO>(sql.ToString(), new { id = id });
                return res.FirstOrDefault();
            }
        }

        //获取登录明细
        public async Task<IEnumerable<UserSginInBO>> GetUserSignInNum()
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                var res = await conn.QueryAsync<UserSginInBO>("select * from LoginSignInInfo where WebType=1");
                return res;
            }
        }

        //登录成功后添加到登录明细里
        public async Task<int> InsertLoginSignInInfo(int id)
        {
            using (SqlConnection conn=new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                var res = await conn.ExecuteAsync($@"insert into LoginSignInInfo values(1,@AdminId,getdate())", new { AdminID = id });
                return res;
            }
        }

    }
    }
